import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class SparseArr {
    /**
     *
     */
    public static void main(String[] args) {
        /** 创建原始数组 **/
        int chessArr1[][] = new int[11][11];
        chessArr1[1][2] = 1;
        chessArr1[2][3] = 2;

        for (int[] ints : chessArr1) {
            for (int item : ints) {
                System.out.printf("%d\t", item);
            }
            System.out.println();
        }

        /** 转稀疏 **/
        int sum = 0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (chessArr1[i][j] != 0) {
                    sum++;
                }
            }
        }

        int[][] sparseArr = new int[sum+1][3];
        sparseArr[0][0] = 11;
        sparseArr[0][1] = 11;
        sparseArr[0][2] = 2;

        int count = 0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (chessArr1[i][j] != 0) {
//                    sparseArr[?][0] = i;
//                    sparseArr[?][1] = j;
//                    sparseArr[?][0] = chessArr1[i][j];
                    sparseArr[++count][0] = i;
                    sparseArr[count][1] = j;
                    sparseArr[count][2] = chessArr1[i][j];
                }
            }
        }

        for (int[] ints : sparseArr) {
            for (int item : ints) {
                System.out.printf("%d\t", item);
            }
            System.out.println();
        }

        /**
         * 稀疏数组存盘
         */
        String filePath = "/Users/tomdayu/home/sparseArr.txt";
        FileWriter writer = null;
        try {
            writer = new FileWriter(filePath);
            for (int[] ints : sparseArr) {
                for (int item : ints) {
                    writer.write(item + "\t");//将int转为String存着
                }
                writer.write("\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        /**
         * 从磁盘读取稀疏数组
         */
//        FileReader reader = null;
        BufferedReader reader = null;
        int[][] sparseArr2 = new int[3][3];

        try {
            String readLine = "";
            reader = new BufferedReader(new FileReader(filePath));
            int i = 0;
            while ((readLine = reader.readLine()) != null) {
                String[] split = readLine.split("\t");
                for (int j = 0; j < split.length; j++) {
                    sparseArr2[i][j] = Integer.valueOf(split[j]);
                }
                i++;
            }
            //打印一下
            System.out.println("开始打印数组：");
            for(int row3[] : sparseArr2) {
                for(int data : row3) {
                    System.out.printf("%d\t",data);
                }
                System.out.printf("\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        /** 将稀疏数组还原为原始数组 **/
//        int chessArr2 [][] = new int[sparseArr[0][0]][sparseArr[0][1]];
        int chessArr2 [][] = new int[sparseArr2[0][0]][sparseArr[0][1]];
        for (int i = 1; i <= sparseArr[0][2]; i++) {
            chessArr2[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
        }

        for (int[] ints : chessArr2) {
            for (int item : ints) {
                System.out.printf("%d\t", item);
            }
            System.out.println();
        }

    }
}
